package org.linlinjava.litemall.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.service.AdminScheduledJobService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallScheduledJob;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统配置-「定时任务」API")
@RestController
@RequestMapping("/admin/scheduled")
@RequiredArgsConstructor
public class AdminScheduledJobController extends ResponseUtil {
    private final AdminScheduledJobService adminScheduledJobService;

    @ApiOperation("定时任务-「列表｜搜索」")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit",value = "limit"),
            @ApiImplicitParam(name = "page",value = "page"),
            @ApiImplicitParam(name = "type",value = "类型"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "name",value = "任务名称")
    })
    @GetMapping("/jobs")
    @LogAnno
    public Response scheduledJobs(@RequestParam("limit")Integer limit,
                                  @RequestParam("page")Integer page,
                                  @RequestParam(value = "type",required = false)Integer type,
                                  @RequestParam(value = "status",required = false)Integer status,
                                  @RequestParam(value = "name",required = false)String name){
        return adminScheduledJobService.scheduledJobs(limit,page,type,status,name);
    }

    @ApiOperation("定时任务-「开关」")
    @ApiImplicitParam(name = "id",value = "id")
    @LogAnno
    @GetMapping("/openOrclose")
    public Response openOrclose(@RequestParam("id")Integer id){
        return adminScheduledJobService.openOrclose(id);
    }

    @ApiOperation("定时任务-「删除」")
    @ApiImplicitParam(name = "id",value = "id")
    @LogAnno
    @GetMapping("/deleted")
    public Response deleted(@RequestParam("id")Integer id){
        return adminScheduledJobService.deleted(id);
    }

    @ApiOperation("定时任务-「批量删除」")
    @ApiImplicitParam(name = "id",value = "id")
    @LogAnno
    @GetMapping("/batchDeleted")
    public Response batchDeleted(@RequestParam("ids") List<Integer> ids){
        return adminScheduledJobService.batchDeleted(ids);
    }

    @ApiOperation("定时任务-「终止任务」")
    @ApiImplicitParam(name = "id",value = "id")
    @LogAnno
    @GetMapping("/abortMission")
    public Response abortMission(@RequestParam("id")Integer id){
        return adminScheduledJobService.abortMission(id);
    }

    @ApiOperation("定时任务-「立即执行一次任务」")
    @ApiImplicitParam(name = "id",value = "id")
    @LogAnno
    @GetMapping("/triggerJob")
    public Response triggerJob(@RequestParam("id")Integer id){
        return adminScheduledJobService.triggerJob(id);
    }

    @ApiOperation("定时任务-「更新任务」")
    @PostMapping("/rescheduleJob")
    @LogAnno
    public Response rescheduleJob(@RequestBody LitemallScheduledJob scheduledJob){
        return adminScheduledJobService.rescheduleJob(scheduledJob);
    }


}
